package com.wujilin.okra.fragment;

import static com.wujilin.okra.Okra.EXTRA_CATEGORY_ID;
import static com.wujilin.okra.Okra.EXTRA_LOADER_ID;
import static com.wujilin.okra.Okra.EXTRA_OBJECTIVE_QUARTER;
import static com.wujilin.okra.Okra.EXTRA_OBJECTIVE_YEAR;
import static com.wujilin.okra.Okra.F_CATEGORY_ID;
import static com.wujilin.okra.Okra.LOADER_OKRA_LIST;
import static com.wujilin.okra.Okra.LOADER_OKRA_LIST_BY_CATEGORY;
import static com.wujilin.okra.Okra.LOADER_OKRA_LIST_BY_QUARTER;
import static com.wujilin.okra.Okra.LOADER_OKRA_LIST_BY_YEAR;
import static com.wujilin.okra.Okra.REQUEST_CATEGORY_CHOOSE;
import static com.wujilin.okra.Okra.REQUEST_OKR_DELETE;
import static com.wujilin.okra.Okra.TAG_DIALOG_CATEGORY_CHOOSE;
import static com.wujilin.okra.Okra.TAG_DIALOG_OKRA_DELETE;
import android.app.Activity;
import android.app.ListFragment;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;

import com.wujilin.okra.R;
import com.wujilin.okra.adapter.OkraListAdapter;
import com.wujilin.okra.database.ModelCursor;
import com.wujilin.okra.loader.QueryCursorLoader;
import com.wujilin.okra.model.Model;

/**
 * @author Belin Wu
 */
public class OkraListFragment extends ListFragment implements LoaderCallbacks<Cursor> {
	private static final String TAG = "OkraListFragment";
	
	private Callback callback;
	private String sql = "select o.*,c.name category from objective o left join category c on o.category_id=c._id";
	
	private ListView listView;
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		callback = (Callback) activity;
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        int loaderId = args.getInt(EXTRA_LOADER_ID);
        getLoaderManager().initLoader(loaderId, args, this);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    	View view = super.onCreateView(inflater, container, savedInstanceState);
    	listView = (ListView) view.findViewById(android.R.id.list);
    	listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    	registerForContextMenu(listView);
    	return view;
    }
    
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
    	callback.onOkraSelected(id);
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    	getActivity().getMenuInflater().inflate(R.menu.fragment_okra_list_context, menu);
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
    	AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    	long objectiveId = info.id;
    	switch (item.getItemId()) {
    		case R.id.move_to: {
    			Model objective = ((ModelCursor) getListAdapter().getItem(info.position)).getModel();
    			long categoryId = objective.getLong(F_CATEGORY_ID);
    			CategoryChooseFragment fragment = CategoryChooseFragment.newInstance(objectiveId, categoryId);
				fragment.setTargetFragment(this, REQUEST_CATEGORY_CHOOSE);
				fragment.show(getFragmentManager(), TAG_DIALOG_CATEGORY_CHOOSE);
    			break;
    		}
    		case R.id.delete: {
				OkraDeleteFragment fragment = OkraDeleteFragment.newInstance(objectiveId);
				fragment.setTargetFragment(this, REQUEST_OKR_DELETE);
				fragment.show(getFragmentManager(), TAG_DIALOG_OKRA_DELETE);
    			break;
    		}
			default:
				return super.onContextItemSelected(item);
    	}
    	return true;
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    	switch (requestCode) {
    		case REQUEST_CATEGORY_CHOOSE:
    			callback.onOkraMoved();
    			break;
    		case REQUEST_OKR_DELETE:
    			callback.onOkraDeleted();
    			break;
    	}
    }
    
	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		switch (id) {
		case LOADER_OKRA_LIST:
			return new QueryCursorLoader(getActivity(), sql);
		case LOADER_OKRA_LIST_BY_CATEGORY:
			long categoryId = args.getLong(EXTRA_CATEGORY_ID);
			return new QueryCursorLoader(getActivity(), sql + " where c._id=?", categoryId + "");
		case LOADER_OKRA_LIST_BY_YEAR: {
			int year = args.getInt(EXTRA_OBJECTIVE_YEAR);
			return new QueryCursorLoader(getActivity(), sql + " where o.year=?", year + "");
		}
		case LOADER_OKRA_LIST_BY_QUARTER:
			int year = args.getInt(EXTRA_OBJECTIVE_YEAR);
			int quarter = args.getInt(EXTRA_OBJECTIVE_QUARTER);
			return new QueryCursorLoader(getActivity(), sql + " where o.year=? and o.quarter=?", year + "", quarter + "");
		}
		return null;
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		OkraListAdapter adapter = new OkraListAdapter(getActivity(), (ModelCursor) data);
		setListAdapter(adapter);
	}

	@Override
	public void onLoaderReset(Loader<Cursor> loader) {
		setListAdapter(null);
	}
	
	/**
	 * 
	 * 
	 * @author Belin Wu
	 *
	 */
	public static interface Callback {
		void onOkraSelected(long id);
		
		void onOkraDeleted();
		
		void onOkraMoved();
	}
}
